#! /usr/bin/env python

from __future__ import print_function
import argparse
import os
import sys

pathname = os.path.abspath(sys.argv[0])
project_root = os.path.dirname(os.path.dirname(pathname))
sys.path = [project_root] + sys.path
import ascii_phonons

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("input_file",
                        help="Path to input file. ASCII formatted for v_sim.")
    parser.add_argument("-m", "--mode_index", default=0, type=int,
                        help="Zero-based position of mode in ASCII file")
    parser.add_argument("-b", "--blender_bin", help="Path to Blender binary")
    parser.add_argument("--camera_rot", type=float,
                        help="View rotation in degrees")
    parser.add_argument("--config", type=str, default='',
                        help="User configuration file")
    parser.add_argument("-d", "--supercell", nargs=3, type=int,
                        help="Supercell dimensions; set of three integers")
    parser.add_argument("--mass_weighting", type=float, default=0.,
                        help="Additional mass-weighting for atom movements. "
                             "This has usually already been done when "
                             "generating the .ascii file, in which case the "
                             "default value of 0 is acceptable. A value of 1 "
                             "corresponds to the formally correct mass "
                             "scaling of m^-0.5 for non-weighted eigenvectors."
                             " A value of -1 will REMOVE existing "
                             "mass-weighting. In-between values may be useful "
                             "for re-scaling to see the range of movements in "
                             "a system, but are not physically meaningful.")
    parser.add_argument("--end_frame", type=int,
                        help="Ending frame number for the animation")
    parser.add_argument("-f", "--n_frames", type=int,
                        help="Number of frames in a complete cycle "
                             "(default number of frames for animation)")
    parser.add_argument("--gif", action="store_true",
                        help="Create a .gif file using Imagemagick convert. "
                             "This flag is ignored if no output file is "
                             "specified.")
    parser.add_argument("-g", "--gui", action="store_true",
                        help="Open full Blender GUI session, even if "
                             "rendering output")
    parser.add_argument("--miller", nargs=3, type=float,
                        help="Miller indices for view")
    parser.add_argument("--montage", action="store_true",
                        help="Create tiled montage with Imagemagick")
    parser.add_argument("--montage_args", type=str,
                        help="Additional args for montage command "
                        "e.g. '-tile {cols}x{rows}'")
    parser.add_argument("--no_box", action="store_true",
                        help="Hide bounding box")
    parser.add_argument("--normalise_vectors", action="store_true",
                        help="Normalise max arrow length per mode")
    parser.add_argument("--offset_box", nargs=3, type=float,
                        help="Bounding box position (lattice coordinates)")
    parser.add_argument("-o", "--output_file",
                        help="Render to output. GUI will not open for further "
                             "editing unless -g (--gui) flag is used")
    parser.add_argument("--orthographic", action="store_true",
                        help="Use orthographic projection "
                             "(i.e. no perspective)")
    parser.add_argument("--start_frame", type=int,
                        help="Starting frame number for the animation")
    parser.add_argument("--scale_arrow", type=float,
                        help="Scale factor for static arrows; 1.0 corresponds"
                        " to about 2 angstroms.")
    parser.add_argument("--scale_atom", type=float,
                        help="Size of atoms, relative to covalent radius. It "
                        "is recommended to reduce this value to less than 0.5"
                        "when using the --vectors flag.")
    parser.add_argument("--scale_vib", type=float,
                        help="Normalised magnitude of animated phonons")
    parser.add_argument("-s", "--static", action="store_true",
                        help="Static image (disable animation)")
    parser.add_argument("-v", "--vectors", action="store_true",
                        help="Indicate eigenvectors with static arrows.")
    parser.add_argument("--zoom", type=float,
                        help="Camera zoom adjustment")

    # Parse args and convert all non-False/zero/none items to a dictionary
    args = parser.parse_args()
    options = {k: v for k, v in vars(args).items() if v}

    user_config = args.config
    if user_config == '':
        pass
    else:
        options['config'] = os.path.abspath(user_config)

    input_file = args.input_file
    options['input_file'] = os.path.abspath(input_file)

    if 'no_box' in options:
        options['show_box'] = False

    opts = ascii_phonons.Opts(options)

    if opts.get('montage', False) and opts.get('static', False):
        ascii_phonons.montage_static(**options)
    elif opts.get('montage', False):
        ascii_phonons.montage_anim(**options)
    else:
        ascii_phonons.call_blender(**options)
